package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.Exception.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 作用:新增套餐
     * 查询套餐是否已经关联类别
     *
     * @param
     */
    @Override
    public long findSetmealWithCategory(long categoryId) {
        final long count = setmealMapper.findSetMealWithCategory(categoryId);
        return count;
    }

    /**
     * 新增菜品
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void saveWithDish(SetmealDto setmealDto) {
        //1. 保存套餐基本信息
        //调用保存的方法
        setmealMapper.saveWithDish(setmealDto);
        //2. 获取套餐关联的菜品集合,为集合中的每一个元素赋值套餐ID
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            return setmealDish;
        }).collect(Collectors.toList());
        //3.批量擦加入套餐
        setmealDishMapper.saveBatch(setmealDishes);


    }

    /**
     * 套餐分页查询
     *
     * @param page     当前页
     * @param pageSize 页面大小
     * @param name     菜品的名称
     * @return
     */
    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //1. 设置当前页和当前页大小
        PageHelper.startPage(page, pageSize);
        //2. 根据name查询数据,得到List集合,List<SetmealDto> select *from setmeal
        List<Setmeal> setmealList = setmealMapper.findByName(name);
        //3. 创建PageInfo对象,只有创建PageInfo 对象了,上面的sql语句才能够执行
        final PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        //4. 遍历List<Setmeal> 把Setmeal转换为SetmealDto
        final List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : pageInfo.getList()) {
            //每一个setmeal对应一个setmaelDto
            final SetmealDto setmealDto = new SetmealDto();
            //属性拷贝
            BeanUtils.copyProperties(setmeal, setmealDto);
            //查询套餐类别
            final Category category = categoryMapper.findById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);

        }
        //5.创建Page对象
        final Page<SetmealDto> pageResult = new Page<>(setmealDtoList, pageInfo.getTotal(), page, pageSize);
        return pageResult;
    }

    /**
     * 作用:批量删除, 若套餐在售无法删除
     *
     * @param ids
     */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void deleteByIds(List<Long> ids) {
        //1.检查判断要删除的套餐是否在售,在售不能删除,思想是查询套餐状态集合中状态(status)为0的数量,若大于0则里面有不能删除的套餐
        Long count = setmealMapper.queryDishWithStatus(ids);
        if (count > 0) {
            //若不能删除则抛出异常
            throw new CustomException("存在在售套餐,无法删除");
        }
        //2. 如果删除套餐都是停售状态,那么久就直接删除
        setmealMapper.deleteById(ids);
        //3. 删除套餐对应的的套餐菜品
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 作用; 根据套餐id查找
     *
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public SetmealDto findById(Long id) {
     //1. 根据套餐id查询查找套餐
       Setmeal setmeal= setmealMapper.findById(id);
       //2. 根据菜品id查询相关的套餐对应的菜品
        List<SetmealDish> setmealDisheList=setmealDishMapper.findByCategoryId(id);
        //3. 创建SetmealDto 把套餐,菜品 疯转的高SetmealDto中
        final SetmealDto setmealDto = new SetmealDto();
        //Copy属性
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDisheList);
        return setmealDto;
    }
    /**
     * 作用;添加菜品套餐
     * @param setmealDto  页面传递过来套餐和菜品
     * @return
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //1. 补全修改时间
        setmealDto.setUpdateTime(LocalDateTime.now());
        //2. 修改菜品
        setmealMapper.updateById(setmealDto);
        //3. 删除套餐的的所有菜品
        List<Long>id= Collections.singletonList(setmealDto.getId());
        setmealDishMapper.deleteBySetmealIds(id);
        //3. 补全套餐菜品信息,然后重新输入
        final List<SetmealDish> dishs = setmealDto.getSetmealDishes();
        final List<SetmealDish> setmealDishList = dishs.stream().map(dish ->{
            //对于一个元素进行加工

            dish.setSetmealId(setmealDto.getId());
            dish.setUpdateUser(setmealDto.getUpdateUser());
            dish.setCreateUser(setmealDto.getCreateUser());
            dish.setUpdateTime(LocalDateTime.now());
            dish.setCreateTime(LocalDateTime.now());
            dish.setSort(0);
            return dish;

        }).collect(Collectors.toList());
        setmealDishMapper.saveBatch(setmealDishList);
    }

    /**
     * 启/停售 (可批量)
     *
     * @param ids
     * @param status
     * @return
     */
    @Override
    public void updateStatus(List<Long> ids, Integer status) {
        setmealMapper.updateStatus(ids,status);
    }

}
